package org.x.note.socket;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.Socket;
import java.net.URISyntaxException;
import java.net.UnknownHostException;

public class SocketClient {

	public static void main(String[] args) throws UnknownHostException,
			IOException, URISyntaxException {
		String s = "123123123123阿斯顿";

		Socket socket = new Socket("127.0.0.1", 9999);
		SocketUtil su = new SocketUtil(socket, "UTF-8");
		su.sendMessage(s, false);
		su.receiveMessage();
	}

	/**
	 * 以字符为单位读取文件，常用于读文本，数字等类型的文件
	 */
	public static String readFileByChars(String fileName) {
		File file = new File(fileName);
		Reader reader = null;
		try {
			// 一次读一个字符
			reader = new InputStreamReader(new FileInputStream(file));
			char[] cbuf = new char[2048];
			int count = reader.read(cbuf, 0, 2048);
			char[] receiveBytes = new char[count];
			System.arraycopy(cbuf, 0, receiveBytes, 0, count);
			reader.close();
			return new String(receiveBytes);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
}
